package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.glowmee.cmd.PouchProductMapCMD;
import com.glowdayz.glowmee.dao.PouchProductMapDAO;
import com.glowdayz.glowmee.dao.RegisterDAO;

public class RequestProductBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public RequestProductBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String where = request.getParameter("where");
		if (where != null) {
			where = Util.getInstance().incoding(where);
		} else {
			where = "";
		}
		
		String isConfirm = request.getParameter("isConfirm");
		String nickName = request.getParameter("nickName");
		String start_date = request.getParameter("start_date");
		String end_date = request.getParameter("end_date");
		if (isConfirm != null && !isConfirm.equals("")) {
			where += " AND m.isConfirm = " + isConfirm;
		}
		if (nickName != null && !nickName.equals("")) {
			nickName = Util.getInstance().incoding(nickName);
			where += " AND r.nickName like '%" + nickName + "%'";
		}
		if (start_date != null && !start_date.equals("") && end_date != null && !end_date.equals("")) {
			start_date = start_date.replace("-", "");
			start_date += "000000";
			end_date += "999999";
			end_date = end_date.replace("-", "");
			where += " AND m.create_date BETWEEN " + start_date + " AND " + end_date;
		}
		
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		
		if (order != null && order.equals("isUnConfirm")) {
			order = "m.isConfirm ASC, m.idPouchProduct DESC";
			request.setAttribute("order", "&order=isUnConfirm");
		} else if (order != null && order.equals("isConfirm")) {
			order = "m.isConfirm DESC, m.idPouchProduct DESC";
			request.setAttribute("order", "&order=isConfirm");
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			
			ArrayList<PouchProductMapCMD> list = PouchProductMapDAO.getInstance().getRequestProductList(page, 10, order, where);
			for (int i=0; i<list.size(); i++) {
				PouchProductMapCMD item = list.get(i);
				if (item.getNickName() == null) {
					int idRegister = item.getIdRegister();
					String name = RegisterDAO.getInstance().getRegisterNickName(idRegister);
					item.setNickName(name);
				}
			}
			
			request.setAttribute("request", list);
			request.setAttribute("display", DBHandler.getInstance().getCount("pouchproductmapping m, pouch p, register r", "*", 
					"m.idPouch=p.idPouch AND p.idRegister=r.idRegister AND m.requestText is not null AND m.isConfirm=1" + where));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("pouchproductmapping m, pouch p, register r", "*", 
					"m.idPouch=p.idPouch AND p.idRegister=r.idRegister AND m.requestText is not null AND m.isConfirm=0" + where));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/requestProduct.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		int idPouchProduct = Integer.parseInt(request.getParameter("idPouchProduct"));
		int product = Integer.parseInt(request.getParameter("product"));
		
		try {
			PouchProductMapDAO.getInstance().updateMappingProduct(idPouchProduct, product);
			request.setAttribute("alert", "<script type='text/javascript'>alert('맵핑이 완료되었습니다.')</script>");
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			
			ArrayList<PouchProductMapCMD> list = PouchProductMapDAO.getInstance().getRequestProductList(page, 10, null, null);
			for (int i=0; i<list.size(); i++) {
				PouchProductMapCMD item = list.get(i);
				if (item.getNickName() == null) {
					int idRegister = item.getIdRegister();
					String name = RegisterDAO.getInstance().getRegisterNickName(idRegister);
					item.setNickName(name);
				}
			}
			
			request.setAttribute("request", list);
			request.setAttribute("display", DBHandler.getInstance().getCount("pouchproductmapping m, pouch p, register r", "*", 
					"m.idPouch=p.idPouch AND p.idRegister=r.idRegister AND m.requestText is not null AND m.isConfirm=1"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("pouchproductmapping m, pouch p, register r", "*", 
					"m.idPouch=p.idPouch AND p.idRegister=r.idRegister AND m.requestText is not null AND m.isConfirm=0"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/requestProduct.jsp");
		dpr.forward(request, response);
	}

}